//
//  EducationViewController.m
//  iBaby365v2
//
//  Created by admin on 14/12/1.
//  Copyright (c) 2014年 suplus. All rights reserved.
//
#define MaxReturnCount     10
#import "EducationViewController.h"
#import "EducationCell.h"
#import "AFNetworking.h"
#import "UIAlertView+Kirinrin.h"
#import "MJExtension.h"
#import "EdudescController.h"
#import "SPEdudescFrame.h"
#import "MBProgressHUD+MJ.h"
#import "MJRefresh.h"
#import "NetworkAccessManager.h"

@interface EducationViewController ()<UITableViewDataSource,UITableViewDelegate,MJRefreshBaseViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *educations;
@property(nonatomic,weak)MJRefreshHeaderView *header;
@property(nonatomic,weak)MJRefreshFooterView *footer;
@property(nonatomic,assign)int firstReturnCount;
@end

@implementation EducationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupRefreshView];
    self.tableView.rowHeight = 215;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    
}


-(void)setupRefreshView
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableView;
    header.delegate = self;
    
    [header beginRefreshing];
    self.header = header;
    
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableView;
    footer.delegate = self;
    self.footer = footer;
}

-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
   NSString *uriString = @"TeachingParentingInformation.json";
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) {
//        上拉刷新
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"key"] = self.appDelegate.accessKey;
        parameters[@"maxReturnCount"] = [NSNumber numberWithInt:MaxReturnCount];
        parameters[@"firstReturnCount"] = [NSNumber numberWithInt:self.firstReturnCount];
        NSLog(@"key====%@",parameters);
        
        [[NetworkAccessManager shareManager] GET:uriString parameters:parameters
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
             
             int resultCode = [responseObject[@"result"]intValue];
             if (resultCode == 0) {
                 //             self.menuData = responseObject[@"data"];
                 NSArray *educationArray = [SPEducation objectArrayWithKeyValuesArray:responseObject[@"data"]];
                 [self.educations addObjectsFromArray:educationArray];
                 NSLog(@"self.educations===%d",self.educations.count);
                 [self.tableView reloadData];
             }else{
                 [UIAlertView alertWithRemoteError:resultCode];
                 
             }
             [self.footer endRefreshing];
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"opernation: %@\nerror: %@", operation, error);
             [UIAlertView alertWithNetworkError];
             [self.footer endRefreshing];
         }];

    } else {
//        下拉刷新
//        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"key"] = self.appDelegate.accessKey;
        parameters[@"maxReturnCount"] = [NSNumber numberWithInt:MaxReturnCount];
        parameters[@"firstReturnCount"] = @0;
        NSLog(@"key====%@",parameters);
        
        [[NetworkAccessManager shareManager] GET:uriString parameters:parameters
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
             
             int resultCode = [responseObject[@"result"]intValue];
             if (resultCode == 0) {
                 //             self.menuData = responseObject[@"data"];
                 NSArray *educationArray = [SPEducation objectArrayWithKeyValuesArray:responseObject[@"data"]];
                 
                 self.educations = [NSMutableArray arrayWithArray:educationArray];
                 
                 NSLog(@"self.educations===%d",self.educations.count);
                 [self.tableView reloadData];
                 self.firstReturnCount = self.educations.count;
                 
             }else{
                 [UIAlertView alertWithRemoteError:resultCode];
                 
             }
             [self.header endRefreshing];
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"opernation: %@\nerror: %@", operation, error);
             [UIAlertView alertWithNetworkError];
             [self.header endRefreshing];
         }];

    }
}



-(void)dealloc
{
    [self.header free];
    [self.footer free];
}


#pragma mark - 数据源方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"self.educations=*****==%d",self.educations.count);
    return self.educations.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EducationCell *cell = [EducationCell cellWithTableView:tableView];
    cell.education = self.educations[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中这行
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    UITableViewController *desccol = [[EdudescController alloc] init];
    [self performSegueWithIdentifier:@"educationToDetil" sender:nil];

        [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id vc = segue.destinationViewController;
    if ([vc isKindOfClass:[EdudescController class]]){
        EdudescController *edc = vc;
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        edc.speducation = self.educations[path.row];
    }
}
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

@end
